/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.service.testimonials.impl;

import com.blandware.atleap.common.util.PartialCollection;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.testimonials.Testimonial;
import com.blandware.atleap.persistence.core.PageDAO;
import com.blandware.atleap.persistence.exception.DeleteException;
import com.blandware.atleap.persistence.testimonials.TestimonialDAO;
import com.blandware.atleap.service.core.impl.PageManagerImpl;
import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
import com.blandware.atleap.service.exception.BeanNotFoundException;
import com.blandware.atleap.service.testimonials.TestimonialManager;

import java.util.Map;

/**
 * <p>Implementation of TestimonialManager</p>
 * <p><a href="TestimonialManagerImpl.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
 * @version $Revision: 1.3 $ $Date: 2008/01/13 17:20:02 $
 */
public class TestimonialManagerImpl extends PageManagerImpl implements TestimonialManager {

    /**
     * Testimonial DAO
     */
	protected TestimonialDAO testimonialDAO;
    /**
     * Page DAO
     */
	protected PageDAO pageDAO;

	/**
	 * Creates new instance of PageManagerImpl
	 */
	public TestimonialManagerImpl() {
	}

    /**
     * Sets DAO for operations with testimonials
     *
     * @param testimonialDAO the DAO to set
     */
	public void setTestimonialDAO(TestimonialDAO testimonialDAO) {
		this.testimonialDAO = testimonialDAO;
	}

    /**
     * Sets DAO for operations with pages
     *
     * @param pageDAO the DAO to set
     */
	public void setPageDAO(PageDAO pageDAO) {
		this.pageDAO = pageDAO;
	}

	//~ CRUD Methods ================================================================

	/**
	 * @see com.blandware.atleap.service.testimonials.TestimonialManager#createTestimonial(com.blandware.atleap.model.testimonials.Testimonial, java.util.Map)
	 */
	public Long createTestimonial(Testimonial testimonial, Map linkedObjects) throws BeanAlreadyExistsException {

		if ( log.isDebugEnabled() ) {
			log.debug("Creating new testimonial item...");
		}

		if ( pageDAO.hasDuplicates(testimonial) ) {
			// linkable item already exists
			String errorMessage = "Linkable item with URI '" + testimonial.getUri() + "' already exists";
			if ( log.isErrorEnabled() ) {
				log.error(errorMessage);
			}
			throw new BeanAlreadyExistsException(errorMessage);
		}

		// linkable item does not exist
		Long testimonialId = testimonialDAO.createTestimonial(testimonial, linkedObjects);
		if ( log.isDebugEnabled() ) {
			log.debug("New testimonial item has been created successfully. Its ID is " + testimonialId);
		}
		return testimonialId;
	}

	/**
	 * @see com.blandware.atleap.service.testimonials.TestimonialManager#retrieveTestimonial(Long)
	 */
	public Testimonial retrieveTestimonial(Long testimonialId) {
		Testimonial testimonial;
		testimonial = testimonialDAO.retrieveTestimonial(testimonialId);

		return testimonial;
	}

	/**
	 * @see com.blandware.atleap.service.testimonials.TestimonialManager#retrieveTestimonialLite(Long)
	 */
	public Testimonial retrieveTestimonialLite(Long testimonialId) {
		return testimonialDAO.retrieveTestimonialLite(testimonialId);
	}

	/**
	 * @see com.blandware.atleap.service.testimonials.TestimonialManager#updateTestimonial(com.blandware.atleap.model.testimonials.Testimonial, java.util.Map)
	 */
	public void updateTestimonial(Testimonial testimonial, Map linkedObjects) throws BeanAlreadyExistsException {

        // remove testimonial from cache in order to prevent Hibernate from assigning new version number
		testimonialDAO.removeFromCache(testimonial);

		if ( log.isDebugEnabled() ) {
			log.debug("Updating testimonial item...");
		}

		if ( pageDAO.hasDuplicates(testimonial) ) {
			// linkable item already exists
			String errorMessage = "Linkable item with URI '" + testimonial.getUri() + "' already exists";
			if ( log.isErrorEnabled() ) {
				log.error(errorMessage);
			}
			throw new BeanAlreadyExistsException(errorMessage);
		}

		// linkable item does not exist
		testimonialDAO.updateTestimonial(testimonial, linkedObjects);

		if ( log.isDebugEnabled() ) {
			log.debug("Testimonial item was updated successfully.");
		}
	}

	/**
	 * @see com.blandware.atleap.service.testimonials.TestimonialManager#updateTestimonial(com.blandware.atleap.model.testimonials.Testimonial)
	 */
	public void updateTestimonial(Testimonial testimonial) throws BeanAlreadyExistsException {
		updateTestimonial(testimonial, null);
	}

	/**
	 * @see com.blandware.atleap.service.testimonials.TestimonialManager#deleteTestimonial(Long)
	 */
	public void deleteTestimonial(Long testimonialId) throws DeleteException, BeanNotFoundException {
		Testimonial testimonial = testimonialDAO.retrieveTestimonial(testimonialId);
		if ( testimonial == null ) {
			String errorMessage = "Testimonial item with ID=" + testimonialId + " does not exist";
			throw new BeanNotFoundException(errorMessage);
		}
		testimonialDAO.deleteTestimonial(testimonial);
		if ( log.isDebugEnabled() ) {
			log.debug("Deleted testimonial item: ID=" + testimonialId);
		}
	}

	// ~ Additional methods ================================================================

	/**
	 * @see com.blandware.atleap.service.testimonials.TestimonialManager#listTestimonials(com.blandware.atleap.common.util.QueryInfo)
	 */
	public PartialCollection listTestimonials(QueryInfo queryInfo) {
		return testimonialDAO.listTestimonials(queryInfo);
	}

	/**
	 * @see com.blandware.atleap.service.testimonials.TestimonialManager#getRandomTestimonial()
	 */
	public Testimonial getRandomTestimonial() {
		return testimonialDAO.getRandomTestimonial();
	}

}
